package com.yuma.all.config;

import com.yuma.all.func.RecruitServiceFunction;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Configuration
public class RagConfig {

    @Bean
    public VectorStore vectorStore(EmbeddingModel embeddingModel){
        SimpleVectorStore simpleVectorStore = SimpleVectorStore.builder(embeddingModel)
                .build();
        // 提取文本內容
        String filePath = "崔济巍简历.txt";
        // 文本字符输入流读取文件内容
        TextReader textReader = new TextReader(filePath);
        //分析文本内容
        textReader.getCustomMetadata().put("filepath", filePath);
        // 返回分析后的文档
        List<Document> documentsList = textReader.get();
        // 段落切分
        TokenTextSplitter splitter = new TokenTextSplitter(1200,350,5,100,true);
        splitter.apply(documentsList);
        // 向量化存儲
        simpleVectorStore.add(documentsList);
        return simpleVectorStore;
    }

    @Bean
    @Description("某某是否有资格面试")
    public Function<RecruitServiceFunction.Request, RecruitServiceFunction.Response> recruitServiceFunction(){
        return new RecruitServiceFunction();
    }

}
